from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import SKU
from goods.serializers import *
from group.models import Cate
from brands.models import BrandsModels
from brands.serializers import BrandsSer
from django.core.paginator import Paginator
# Create your views here.

# 获取SKU所有信息
class GetSKUViews(APIView):
    def get(self,request):
        # 接受前端数据
        currentPage = request.query_params.get('pagesize')
        page = request.query_params.get('page')
        print('接受数据',currentPage,page)

        sku = SKU.objects.all()
        count = sku.count()

        pag = Paginator(sku, currentPage)
        dada = pag.page(page)

        # 序列化输出
        ser = SKUSer(dada,many=True)

        return Response({
            'data':ser.data,
            'count':count
        })



    def post(self,request):
        # 接受前端数据
        name = request.data.get('name')
        # 外键
        spu = request.data.get('spu')
        # 副标题
        describe = request.data.get('describe')
        # 分类
        cate = request.data.get('cate')
        # 库存
        stock = request.data.get('stock')
        # 是否上架
        is_selling = request.data.get('is_selling')
        price = request.data.get('price')
        sales = request.data.get('sales')

        print('dasdsa',is_selling)

        if not all([name,spu,describe,price,cate,stock,is_selling]):
            return Response({
                'code':400,
                'msg':'没有接受到前端信息'
            })
        try:
            SKU.objects.create(
                name=name,
                spu_id=spu,
                describe=describe,
                cate_id=cate,
                stock=stock,
                is_selling='True',
                price=price,
                sales=sales,
            )
        except Exception as e:
            print('dasdasf',e)
            return Response({
                'code':400,
                'msg':'添加失败'
            })
        return Response({
            'code':201,
            'msg':'添加成功'
        })


# 获取和添加SPU
class SPUView(APIView):
    def get(self,request):
        currentPage = request.query_params.get('pagesize')
        page = request.query_params.get('page')
        print('dasdasa大赛',currentPage,page)
        spu = SPU.objects.all()
        wang = spu.count()
        pag = Paginator(spu,currentPage)
        dada = pag.page(page)
        ser = SpuSer(dada,many=True)
        return Response({
            'data':ser.data,
            'count':wang
        })

    def post(self,request):
        # 接受前端数据
        name = request.data.get('name')
        brand = request.data.get('brand')
        category1 = request.data.get('category1')
        category2 = request.data.get('category2')
        category3 = request.data.get('category3')
        desc_detail = request.data.get('desc_detail')
        desc_pack = request.data.get('desc_pack')
        desc_service = request.data.get('desc_service')

        print('小达达',name,brand,category1,category2,category3,desc_detail,desc_pack,desc_service)

        if not all([name,brand,category1,category2,category3,desc_detail,desc_pack,desc_service]):
            return Response({
                'code':400,
                'msg':"没有接受到前端数据"
            })

        SPU.objects.create(
            name=name,
            brand_id=brand,
            category1_id=category1,
            category2_id=category2,
            category3_id=category3,
            desc_detail=desc_detail,
            desc_pack=desc_pack,
            desc_service=desc_service
        )

        return Response({
            'code':200,
            'msg':'添加成功'
        })



# 修改和删除SPU信息

class SpusXView(APIView):
    def get(self,request,id):
        try:
            sku = SPU.objects.get(id=id)
        except Exception as e:
            print('没有SKU数据',e)
            return Response({
                'code':400,
                'msg':'没有收到SKU数据id'
            })

        ser = SpuSer(sku)

        return Response(ser.data)



    # 修改
    def put(self,request,id):
        name = request.data.get('name')
        brand_name = request.data.get('brand_name')
        category1 = request.data.get('category1')
        category2 = request.data.get('category2')
        category3 = request.data.get('category3')
        desc_detail = request.data.get('desc_detail')
        desc_pack = request.data.get('desc_pack')
        desc_service = request.data.get('desc_service')

        print('小登达', name, brand_name, category1, category2, category3, desc_detail, desc_pack, desc_service)

        if not all([name, brand_name, category1, category2, category3, desc_detail, desc_pack, desc_service]):
            return Response({
                'code': 400,
                'msg': "没有接受到前端数据"
            })
        try:
            spu = SPU.objects.get(id=id)
        except Exception as e:
            print('没有此id',e)
            return Response({
                'code':400,
                'msg':'没有此id'
            })

        # 根据品牌名字查询品牌di
        try:
            brand = BrandsModels.objects.get(name=brand_name)
        except Exception as e:
            print('没有此品牌',e)
            return Response({
                'code':400,
                'msg':'没有此品牌'
            })

        # 所属品牌id
        brand_id = brand.id

        # 根据二级分类名字查询二级分类di
        try:
            category2_name = Cate.objects.get(name=category2)
        except Exception as e:
            print('没有此品牌', e)
            return Response({
                'code': 400,
                'msg': '没有此品牌'
            })

        # 根据二级分类名字查询二级分类di
        category2_id = category2_name.id

        # 根据三级分类名字查询三级分类di
        try:
            category3_name = Cate.objects.get(name=category3)
        except Exception as e:
            print('没有此品牌', e)
            return Response({
                'code': 400,
                'msg': '没有此品牌'
            })

        # 根据三级分类名字查询三级分类di
        category3_id = category3_name.id


        try:
            spu.name = name
            spu.brand_id = brand_id
            spu.category1_id = category1
            spu.category2_id = category2_id
            spu.category3_id = category3_id
            spu.desc_detail = desc_detail
            spu.desc_pack = desc_pack
            spu.desc_service = desc_service
            spu.save()
            return Response({
                'code': 200,
                'msg': '修改成功'
            })
        except Exception as e:
            print('添加错误',e)
            return Response({
                'code':400,
                'msg':'添加失败'
            })


    # 删除
    def delete(self,request,id):
        print('小达达是的',id)
        try:
            SPU.objects.get(id=id).delete()
            return Response({
                'code':204,
                'msg':'删除成功'
            })
        except Exception as e:
            print('删除错误',e)
            return Response({
                'code':400,
                'msg':'删除错误'
            })







class PutDelView(APIView):
    def get(self,request,id):
        try:
            sku = SKU.objects.get(id=id)
        except Exception as e:
            print('大萨达',e)
            return Response({
                'code':400,
                'msg':'没有此数据'
            })
        ser = SKUSer(sku)
        return Response(ser.data)


    def put(self,request,id):
        # 接受前端数据
        name = request.data.get('name')
        # 外键
        spu = request.data.get('spu')
        # 副标题
        describe = request.data.get('describe')
        # 分类
        cate = request.data.get('cate')
        # 库存
        stock = request.data.get('stock')
        # 是否上架
        is_selling = request.data.get('is_selling')
        price = request.data.get('price')
        sales = request.data.get('sales')
        print('销量',sales)

        print('dasdsa', is_selling,name,spu,describe,cate,stock,price,sales)

        if not all([name, spu, describe, price, cate, stock, is_selling]):
            return Response({
                'code': 400,
                'msg': '没有接受到前端信息'
            })

        try:
            sku = SKU.objects.get(id=id)
        except Exception as e:
            return Response({
                'code':400,
                'msg':'没有这个数据'
            })

        try:
            sku.name = name
            sku.spu_id = spu
            sku.describe = describe
            sku.cate_id = cate
            sku.stock = stock
            sku.is_selling = is_selling
            sku.price = price
            sku.sales = sales
            sku.save()
            return Response({
                'code': 201,
                'msg': '修改成功'
            })
        except Exception as e:
            print('修改错误',e)
            return Response({
                'code':400,
                'msg':'修改错误'
            })




    def delete(self,request,id):
        try:
            SKU.objects.get(id=id).delete()
        except Exception as e:
            print('删除错误',e)
            return Response({
                'code':400,
                'msg':'删除失败'
            })
        return Response({
            'code':204,
            'msg':'删除成功'
        })

# 根据一级类别查询二级类别
class CategoriesView(APIView):
    def get(self,request,id):
        # 一级类别id
        try:
            cate = Cate.objects.get(id=id)
        except Exception as e:
            print('错误信息',e)
            return Response({
                'code':400,
                'msg':'没有此id'
            })

        cate1 = Cate.objects.filter(cid_id=cate.id)

        print('dasdasf',cate1)

        ser = []

        for i in cate1:
            adad = {
                'id': i.id,
                'name': i.name,
                'cid': i.cid.id,
            }
            ser.append(adad)

        return Response(ser)


# 获取所有品牌信息

class CrandsView(APIView):
    def get(self,request):
        brands = BrandsModels.objects.all()

        ser = BrandsSer(brands,many=True)

        return Response(ser.data)





# 权限类型获取



